package com.allen.retrofit2_rxjava2.view.selectImage;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.CancellationSignal;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.AppCompatCheckBox;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.allen.retrofit2_rxjava2.R;
import com.allen.retrofit2_rxjava2.app.MyApp;
import com.allen.retrofit2_rxjava2.base.TitleViewActivity;
import com.allen.retrofit2_rxjava2.view.selectImage.SelectPhotoActivity;
import com.allen.retrofit2_rxjava2.view.selectImage.adapter.GalleryPagerAdapter;
import com.allen.retrofit2_rxjava2.view.selectImage.adapter.UploadVideoImageAdapter;
import com.allen.retrofit2_rxjava2.view.selectImage.entity.ChooseUploadParam;
import com.allen.retrofit2_rxjava2.view.selectImage.view.HackyProblematicViewPager;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 图片预览
 */
public class PreviewActivity extends TitleViewActivity implements View.OnClickListener, ViewPager.OnPageChangeListener, GalleryPagerAdapter.OnVideoItemClickListener {

    private static final String TAG = "preview";

    private HackyProblematicViewPager vp_gallery;
    private GalleryPagerAdapter adapter;
    private ArrayList<ChooseUploadParam> selectingParams;
    private ImageView bt_img_photo_delete;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_preview);
        initView();
        initHeader();
        initData();
    }

    private void initHeader() {
        initTitleView();
        initLeftBackView(null);
        setRightViewVisible(false);
        setCenterViewContent(getResources().getString(R.string.preview));
    }

    private void initView() {
        vp_gallery = (HackyProblematicViewPager) findViewById(R.id.vp_gallery);
        bt_img_photo_delete = findViewById(R.id.bt_img_photo_delete);
        vp_gallery.addOnPageChangeListener(this);
        bt_img_photo_delete.setOnClickListener(this);
    }

    private void initData() {
        Intent intent = getIntent();
        int initPosition = intent.getIntExtra("initPosition", 0);
        selectingParams = intent.getParcelableArrayListExtra("select");
        adapter = new GalleryPagerAdapter(this, selectingParams);
        adapter.setOnVideoItemClickListener(this);
        vp_gallery.setAdapter(adapter);
        vp_gallery.setCurrentItem(initPosition);
        setCenterViewContent((initPosition + 1) + "/" + selectingParams.size());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.bt_img_photo_delete:
                int position = vp_gallery.getCurrentItem();
                removeParam(position);
                break;


            case R.id.bt_img_photo_share:
//                int position = vp_gallery.getCurrentItem();
//                ChooseUploadParam param = datas.get(position);
//                removeParam(param);
                break;
        }
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN) {
            if (PreviewActivity.this.isFinishing()) {
                return true;
            }
            returnTop();
            return true;
        }
        return super.dispatchKeyEvent(event);
    }


    private void returnTop() {
        Intent intent = getIntent();
        intent.putParcelableArrayListExtra("data", selectingParams);
        setResult(RESULT_OK, intent);
        finish();
    }

    private void removeParam(int index) {
        selectingParams.remove(index);
        Toast.makeText(this, ":" + selectingParams.size(), Toast.LENGTH_SHORT).show();
        adapter.notifyDataSetChanged();
        vp_gallery.invalidate();
        if (adapter.getCount() == 0) {
            selectingParams.clear();
            returnTop();
        }
    }

    private void playVideo(String path) {
        Log.d(TAG, "要播放的视频地址：" + path);
        Intent intent = new Intent(Intent.ACTION_VIEW);
        String type = "video/*";
        Uri uri = Uri.fromFile(new File(path));
        intent.setDataAndType(uri, type);
        startActivity(intent);
    }

    @Override
    public void onPageSelected(int position) {
        ArrayList<ChooseUploadParam> params = adapter.getParams();
        setCenterViewContent((position + 1) + "/" + params.size());
    }


    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    public void onVideoItemClick(String path) {
        playVideo(path);
    }


    public boolean over2Video() {
        int videoCount = 0;
        for (ChooseUploadParam param : selectingParams) {
            if (param.getType() == UploadVideoImageAdapter.TYPE_VIDEO) {
                videoCount++;
            }
        }
        return videoCount > 1;
    }
}
